﻿using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.BaseImp;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 企业服务实现
    /// </summary>
    public class EnterpriseService : IEnterpriseService
    {

        /// <summary>
        /// 企业仓储
        /// </summary>
        private readonly IBaseRepository<Enterprise> repository;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">企业仓储</param>
        public EnterpriseService(IBaseRepository<Enterprise> repository)
        {
            this.repository = repository;
         
        }


        /// <summary>
        /// 新增企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public async Task<int> AddEnterprises(Enterprise enterprise)
        {
          
            return repository.Add(enterprise);
        }

        /// <summary>
        /// 删除企业
        /// </summary>
        /// <param name="EnterpriseId"></param>
        /// <returns></returns>
        public int DeleteEnterprises(int EnterpriseId)
        {
            return repository.Del(EnterpriseId);
        }

        

        /// <summary>
        /// 查询加分页
        /// </summary>
        /// <param name="EnterpriseName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<PageOutput<Enterprise>> GetEnterprises(string? EnterpriseName, int pageIndex, int pageSize)
        {
            var list= repository.GetAll().ToList();  

            if(!string.IsNullOrWhiteSpace(EnterpriseName))
            {
                list=list.Where(c=>c.EnterpriseName.Contains(EnterpriseName)).ToList(); 
            }
            var result = new PageOutput<Enterprise>()
            {
                 Total = list.Count(),
                 PageSize = pageSize,
            };
            var entity=list.OrderByDescending(c=>c.EnterpriseId).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            result.Data = entity;
            return result;
           
        }

    }
}
